from django.test import TestCase
from database.trace import deal_template, get_grouped_trace, calculate_duration, choose_index_template, timezone_adjust, get_query_command, TraceConnection
from database import trace_conn

import json
from datetime import datetime, timedelta
import os

class TraceConnectionTestCase(TestCase):
    def test_get_grouped_trace(self):
        test_traces = [
            {
                "_source": {
                    "trace": {
                        "id": "trace1",
                        "name": "Trace One"
                    },
                    "data": "Some data for trace 1"
                }
            }
        ]
        result = {'trace1': [{'trace': {'id': 'trace1', 'name': 'Trace One'}, 'data': 'Some data for trace 1'}]}
        self.assertEqual(get_grouped_trace(test_traces), result)
    
    def test_calculate_duration(self):
        trace = [
            {"timestamp": {"us": 100}, "processor": {"event": "start", "duration": {"us": 50}}},
            {"timestamp": {"us": 200}, "processor": {"event": "end", "duration": {"us": 100}}}
        ]
        self.assertEqual(calculate_duration(trace), -100)
    
    def test_choose_index_template(self):
        start_time = 1716048000.0
        end_time = 1716048600.0
        indices = [".ds-traces-apm-default-2024:05:29"]
        choose_index_template(indices, start_time, end_time)

    def test_timezone_adjust(self):
        local_time = datetime(2024, 5, 19, 0, 0, 0)
        result = "2024-05-19T00:00:00.000000Z"
        self.assertEqual(timezone_adjust(local_time), result)

    def test_get_query_command(self):
        start_time = datetime(2024, 5, 19, 0, 0, 0)
        end_time = datetime(2024, 5, 19, 0, 10, 0)
        sort = {"@timestamp": {"order": "asc"}}
        query_size = 7500
        result = {
            "query": {
                "bool": {
                    "must": [
                        {"range": {"@timestamp": {"gte": datetime(2024, 5, 19, 0, 0, 0), "lte": datetime(2024, 5, 19, 0, 10, 0)}}}
                    ]
                }
            },
            "sort": {"@timestamp": {"order": "asc"}},
            "size": 7500,
        }
        self.assertEqual(get_query_command(start_time, end_time, sort, query_size), result)

    def test_TraceConnection_init(self):
        try:
            config = {
                "url": "https://1.92.152.201:9200",
                "username": "elastic",
                "password": "elastic",
            }
            trace_conn1 = TraceConnection(config)
        except Exception as e:
            self.fail(f'test_TraceConnection_init raised an exception: {e}')
    
    def test_TraceConnection_parse_time(self):
        pass
    
    def test_TraceConnection_query_template(self):
        index = "index1"
        query = {
            "query": {
                "bool": {
                    "must": [
                        {"range": {"@timestamp": {"gte": datetime(2024, 5, 19, 0, 0, 0), "lte": datetime(2024, 5, 19, 0, 10, 0)}}}
                    ]
                }
            },
            "sort": {"@timestamp": {"order": "asc"}},
            "size": 7500,
        }

        # result = {}
        # self.assertEqual(trace_conn.query_template(index, query), result)
    
    def test_TraceConnection_multi_threads_query(self):
        query = {
            "query": {
                "bool": {
                    "must": [
                        {"range": {"@timestamp": {"gte": datetime(2024, 5, 19, 0, 0, 0), "lte": datetime(2024, 5, 19, 0, 10, 0)}}}
                    ]
                }
            },
            "sort": {"@timestamp": {"order": "asc"}},
            "size": 7500,
        }
        # result = {}
        # self.assertEqual(trace_conn.multi_threads_query(query), result)
    
    def test_TraceConnection_trace_topology(self):
        end_time = datetime(2024, 5, 19, 0, 10, 0)
        # result = {}
        # self.assertEqual(trace_conn.trace_topology(end_time), result)
    
    def test_TraceConnection_query(self):
        start_time = datetime(2024, 5, 19, 0, 0, 0)
        end_time = datetime(2024, 5, 19, 0, 10, 0)

        # result = {}
        # self.assertEqual(trace_conn.query(start_time, end_time), result)
    
    def test_TraceConnection_trace_byId(self):
        trace_id = "trace_id1"
        # result = {}
        # self.assertEqual(trace_conn.trace_byId(trace_id), result)
    
    def test_TraceConnection_trace_extract(self):
        start_time = datetime(2024, 5, 19, 0, 0, 0)
        end_time = datetime(2024, 5, 19, 0, 10, 0)
        # result = {}
        # self.assertEqual(trace_conn.trace_extract(start_time, end_time), result)
    
    def test_TraceConnection_trace_processing(self):
        traces = {
            "trace":
                {
                    "_source": {
                        "trace": {
                            "id": "trace1",
                            "name": "Trace One"
                        },
                        "data": "Some data for trace 1"
                    }
                }
        }
        # result = {}
        # self.assertEqual(trace_conn.trace_processing(traces), result)
    
    def test_TraceConnection_topology_data(self):
        traces = {
            "trace":
                {
                    "_source": {
                        "trace": {
                            "id": "trace1",
                            "name": "Trace One"
                        },
                        "data": "Some data for trace 1"
                    }
                }
        }
        # result = {}
        # self.assertEqual(trace_conn.topology_data(traces), result)
    
    def test_TraceConnection_trace_data(self):
        # "health" in span[processor_name]["name"] and "POST unknown route" == span[processor_name]["name"]
        # status != "Error" and status_code != 200 and status_code != 0
        # operation_name != "POST unknown route"
        traces = {
            "trace":
                {
                    "_source": {
                        "trace": {
                            "id": "trace1",
                            "name": "Trace One"
                        },
                        "data": "Some data for trace 1"
                    }
                }
        }
        # result = {}
        # self.assertEqual(trace_conn.trace_data(traces), result)
    
    def test_TraceConnection_traceId_data(self):
        trace_id = "trace_id1"
        traces = {
            "trace":
                {
                    "_source": {
                        "trace": {
                            "id": "trace1",
                            "name": "Trace One"
                        },
                        "data": "Some data for trace 1"
                    }
                }
        }
        # result = {}
        # self.assertEqual(trace_conn.traceId_data(traces, trace_id), result)